﻿//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Logging Application Block
//===============================================================================

using System.Diagnostics;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder;
using Data = Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners;
using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging.Formatters;
using Microsoft.Practices.ObjectBuilder2;

namespace EntLibContrib.Logging.Database.Configuration
{
	/// <summary>
	/// This type supports the Enterprise Library infrastructure and is not intended to be used directly from your code.
	/// Represents the process to build a <see cref="ExtendedDatabaseTraceListener"/> described by a <see cref="ExtendedDatabaseTraceListenerData"/> configuration object.
	/// </summary>
	/// <remarks>This type is linked to the <see cref="ExtendedDatabaseTraceListenerData"/> type and it is used by the <see cref="TraceListenerCustomFactory"/> 
	/// to build the specific <see cref="TraceListener"/> object represented by the configuration object.
	/// </remarks>
	public class ExtendedDatabaseTraceListenerAssembler : TraceListenerAsssembler
	{
		/// <summary>
		/// This method supports the Enterprise Library infrastructure and is not intended to be used directly from your code.
		/// Builds a <see cref="ExtendedDatabaseTraceListener"/> based on an instance of <see cref="ExtendedDatabaseTraceListenerData"/>.
		/// </summary>
		/// <seealso cref="TraceListenerCustomFactory"/>
		/// <param name="context">The <see cref="IBuilderContext"/> that represents the current building process.</param>
		/// <param name="objectConfiguration">The configuration object that describes the object to build. Must be an instance of <see cref="ExtendedDatabaseTraceListenerData"/>.</param>
		/// <param name="configurationSource">The source for configuration objects.</param>
		/// <param name="reflectionCache">The cache to use retrieving reflection information.</param>
		/// <returns>A fully initialized instance of <see cref="ExtendedDatabaseTraceListener"/>.</returns>
		public override TraceListener Assemble(IBuilderContext context,
																					 TraceListenerData objectConfiguration,
																					 IConfigurationSource configurationSource,
																					 ConfigurationReflectionCache reflectionCache)
		{
			ExtendedDatabaseTraceListenerData castedObjectConfiguration
					= (ExtendedDatabaseTraceListenerData)objectConfiguration;

			IBuilderContext databaseContext
					= context.CloneForNewBuild(
							NamedTypeBuildKey.Make<Data.Database>(castedObjectConfiguration.DatabaseInstanceName), null);

			Data.Database database
					= (Data.Database)databaseContext.Strategies.ExecuteBuildUp(databaseContext);

			ILogFormatter formatter
					= GetFormatter(context, castedObjectConfiguration.Formatter, configurationSource, reflectionCache);

			TraceListener createdObject
					= new ExtendedDatabaseTraceListener(
							database,
							castedObjectConfiguration.WriteLogSqlCommand,
							castedObjectConfiguration.AddCategorySqlCommand,
							castedObjectConfiguration.CommandType,
							formatter);

			return createdObject;
		}
	}
}
